<template>
  <div id="userRegister">
    <h2 class="title">云图库-用户注册</h2>
    <div class="desc">企业级智能协同云图库</div>
    <a-form :model="formState" name="RegisterForm" autocomplete="off" @finish="submit">
      <a-form-item name="account" :rules="[{ required: true, message: '请输入账号' }]">
        <a-input v-model:value="formState.account" placeholder="请输入账号" />
      </a-form-item>

      <a-form-item
        name="password"
        :rules="[
          { required: true, message: '请输入密码' },
          { min: 8, message: '密码长度不能小于8位' },
        ]"
      >
        <a-input-password v-model:value="formState.password" placeholder="请输入密码" />
      </a-form-item>

      <a-form-item
        name="rePassword"
        :rules="[
          { required: true, message: '请输入确认密码' },
          { min: 8, message: '确认密码长度不能小于8位' },
        ]"
      >
        <a-input-password v-model:value="formState.rePassword" placeholder="请输入确认密码" />
      </a-form-item>

      <div class="tips">
        已有账号?
        <router-link to="/user/login">去登录</router-link>
      </div>

      <!--      提交按钮必须添加html-type-->
      <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
        <a-button type="primary" html-type="submit">注册</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { message } from 'ant-design-vue'
import router from '@/router'
import { userRegisterUsingPost } from '@/api/userController.ts'

const formState = reactive<API.UserRegisterRequestDTO>({
  account: '',
  password: '',
  rePassword: '',
})

//页面上点击提交按钮触发，调用后端注册接口
const submit = async (values: any) => {
  //调用后端注册接口
  const resp = await userRegisterUsingPost(values)
  if (resp.data.code === 0) {
    message.success('注册成功')
    //跳转回登录页
    router.push({ path: '/user/login', replace: true })
  } else {
    message.error(resp.data.message)
  }
}
</script>

<style scoped>
#userRegister {
  max-width: 360px;
  margin: 0 auto;
}

.title {
  text-align: center;
  margin-bottom: 16px;
}

.desc {
  text-align: center;
  margin-bottom: 16px;
  color: #bbb;
}

.tips {
  text-align: center;
  margin-bottom: 16px;
  font-size: 13px;
  color: #bbb;
}
</style>
